﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.Common;

namespace CIT.WMS.Msd
{
    public partial class MSD_IN_Drying : Form
    {
        DataTable dtMsd = new DataTable();

        public MSD_IN_Drying()
        {
            InitializeComponent();
        }

        private void txtSerialNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                string msg = string.Empty;
                if (!ValidateInput(out msg))
                {
                    Common.PublicUtils.ShowMsg(this.lab_msg, msg, false);
                    txtSerialNumber.SelectAll();
                    txtSerialNumber.Focus();
                    return;
                }
                PublicUtils.WriteLog(txtSerialNumber.Text.Trim(), "进干燥箱", ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["MaterialCode"]), ConvertUtils.ChangeNullToInt(dtMsd.Rows[0]["QTY"], 0));
                PublicUtils.ShowMsg(this.lab_msg, msg, true);
                txtSerialNumber.SelectAll();
                txtSerialNumber.Focus();
            }

        }

        private bool ValidateInput(out string msg)
        {
            if (string.IsNullOrEmpty(txtSerialNumber.Text.Trim()))
            {
                msg = "Reelid不能为空";
                return false;
            }
            dtMsd = MSDService.Query(string.Format(" AND a.SerialNumber='{0}'", txtSerialNumber.Text.Trim()));
            if (dtMsd.Rows.Count == 0)//校验库存表是否存在该条码
            {
                msg = "Reelid错误";
                return false;
            }
            if (ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["IsMSD"]) != "0")//校验是否是MSD
            {
                msg = "Reelid非MSD";
                return false;
            }
            if (ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["Status"]) == "5")//4.正在烘烤的物料不允许入干燥箱
            {
                msg = "Reelid烘烤中，无法进入干燥箱";
                return false;
            }
            if (ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["Status"]) == "2")//5.拆封的物料才能入干燥箱
            {
                msg = "Reelid密封状态，无法进入干燥箱";
                return false;
            }
            if (ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["Status"]) == "3")//3.请勿重复进入干燥箱
            {
                msg = "Reelid已在干燥箱，请勿重复进入";
                return false;
            }
            if (string.IsNullOrEmpty(ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["Status"])))//未拆封过
            {
                msg = "Reelid未拆封过，无法进入干燥箱";
                return false;
            }
            MSDService.IN_Drying(txtSerialNumber.Text.Trim(), dtMsd.Rows[0]["ExposeTime"].ToString());
            msg = "进干燥箱成功";
            return true;
        }
    }
}